package com.itheima.mq;

import com.itheima.health.utils.sms.SmsUtils;
import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class SmsConsumer {

    @RabbitListener(queues = "heima-queue")
    public void receive(Map map, Channel channel, Message message) throws IOException {
        long deliveryTag=0L;
        try {
            //会话唯一ID
            deliveryTag  = message.getMessageProperties().getDeliveryTag();
//            System.out.println(1/0);
            String telephone = (String)map.get("telephone");
            String code = (String)map.get("code");
            System.out.println(code+"----"+telephone);
//            SmsUtils.validUserTelephone(telephone,code);
            channel.basicAck(deliveryTag,false);//  手动处理ack  通知broker 删除当前消息
        }catch (Exception e){
            e.printStackTrace();
            channel.basicNack(deliveryTag,false,true);//  第三个参数 ： true  表示 出现代码业务一次 消息 重新放回队列中 再次获取
    }
    }
}
